package com.comtrade.ilserver.tasks;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;
/** 
 * BeaconServer class represent a beacon in space <br/>
 * example json:<br/>
 * 
 * {<br/>
 * "_id":13, <br/>
 * "_uuid":"dec43c9d-04ed-4694-9a11-7ec1c85676e4", <br/>
 * "_version":0, <br/>
 * "macAddress":"CF:A2:1C:85:CD:60", <br/>
 * "x":260.0, <br/>
 * "y":460.0, <br/>
 * "z":100.0, <br/>
 * "angle":90, <br/>
 * "message":"CF:A2:1C:85:CD:60", <br/>
 * "spaceId":640 <br/>}
 *
 */



/////// OBAVEZNO PROMENITI DA SA SERVERA KOORDINATE PROMENI U METRE A NE DA HVATA CM
public class BeaconServer implements Parcelable{
	
	public final static String JSON_KEY_ID = "_id";
	public final static String JSON_KEY_UUID = "_uuid";
	public final static String JSON_KEY_VERSION = "_version";

	public final static String JSON_KEY_X = "x";
	public final static String JSON_KEY_Y = "y";
	public final static String JSON_KEY_Z = "z";
	public final static String JSON_KEY_DESC = "message";
	public final static String JSON_KEY_SID = "spaceId";
	public final static String JSON_KEY_MAC = "macAddress";
	
	private int id;
	private String uuid;
	private int version;
	private String msg;
	private int x, y,z;
	private int sid;
	private String macadd;
	
	public BeaconServer(int _id, String _uuid, int _version, int x, int y, int z,
			String description, int spaceId, String mac) {
		super();
		this.id = _id;
		this.uuid = _uuid;
		this.version = _version;
		this.x = x;
		this.y = y;
		this.z=z;
		this.msg=description;
		this.sid=spaceId;
		this.macadd=mac;
	}
	
	public BeaconServer(Parcel source){

		this.id = source.readInt(); 
		this.uuid=source.readString();
		this.version=source.readInt();
		this.x=source.readInt();
		this.y=source.readInt();
		this.z=source.readInt();
		this.msg=source.readString();
		this.sid=source.readInt();
		this.macadd=source.readString();
	}

	
	public static BeaconServer fromJSON(String json){
		try {
			JSONObject jsonObj = new JSONObject(json);
			int id = jsonObj.getInt(JSON_KEY_ID);
			String uuid = jsonObj.getString(JSON_KEY_UUID);  
			int version = jsonObj.getInt(JSON_KEY_VERSION);
			int x = jsonObj.getInt(JSON_KEY_X);
			int y = jsonObj.getInt(JSON_KEY_Y);
			int z=jsonObj.getInt(JSON_KEY_Z);
			String msg=jsonObj.getString(JSON_KEY_DESC);
			int sid=jsonObj.getInt(JSON_KEY_SID);
			String macadd = jsonObj.getString(JSON_KEY_MAC);
			
			return new BeaconServer(id, uuid, version, x, y,z,msg,sid,macadd);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
	

	
	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(id);
		dest.writeString(uuid);
		dest.writeInt(version);
		dest.writeInt(x);
		dest.writeInt(y);
		dest.writeInt(z);
		dest.writeString(msg);
		dest.writeInt(sid);
		dest.writeString(macadd);

	}
	
	public static Parcelable.Creator<BeaconServer> CREATOR = new Parcelable.Creator<BeaconServer>() {

		@Override
		public BeaconServer[] newArray(int size) {
			return new BeaconServer[size];
		}

		@Override
		public BeaconServer createFromParcel(Parcel source) {
			return new BeaconServer(source);
					
		}
	};

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getZ() {
		return z;
	}

	public void setZ(int z) {
		this.z = z;
	}

	public int getSid() {
		return sid;
	}

	public void setSid(int sid) {
		this.sid = sid;
	}

	public String getMacadd() {
		return macadd;
	}

	public void setMacadd(String macadd) {
		this.macadd = macadd;
	}

	
	

}
